<template>
  <el-container class="home_container">
    <!-- 头部 -->
    <el-header>
      <div class="logo">
        <img src="../assets/1.jpg" alt="" />
        <span>你在搞爪子呦</span>
      </div>
      <el-button type="primary" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 导航菜单 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 展开按钮 -->
        <div @click="isCollapse = !isCollapse" class="toggleMenu">||</div>
        <el-menu
          background-color="rgb(25, 30, 177)"
          active-text-color="#ffd04b"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activeewrew"
        >
          <!-- 一级菜单 -->
          <!-- 用户管理 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menusList"
            :key="item.id"
          >
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconList[item.id]"></i>
              <!-- 文本 -->
              <span style="color: #fff">{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + childItem.path"
              v-for="childItem in item.children"
              :key="childItem.id"
            >
              <!-- 二级菜单模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span style="color: #fff">{{ childItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右边内容区域 -->
      <el-main>
        <!-- 路由占位 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menusList: [],
      iconList: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activeewrew: this.$route.path
    }
  },

  created() {
    this.getMenus()
  },

  methods: {
    logout() {
      this.$router.push('/login')
      sessionStorage.removeItem('token')
    },
    // 获取左侧菜单数据请求
    async getMenus() {
      // 获取数据
      const { data: res } = await this.$axios.get('/menus')
      // 如果获取数据成功，将数据保存到data上
      if (res.meta.status !== 200) return this.$message.error('失败')

      this.menusList = res.data
      console.log(this.menusList)
    }
  }
}
</script>

<style scoped lang='less'>
.home_container {
  height: 100%;
}
.el-menu {
  border-right: none;
}
.el-header {
  background: -webkit-linear-gradient(left, rgb(38, 38, 177), rgb(150, 38, 38));
  display: flex;
  justify-content: space-between;
  align-items: center;
  .logo {
    display: flex;
    align-items: center;
    img {
      width: 50px;
      height: 50px;
      border-radius: 50%;
    }
    span {
      margin-left: 15px;
      color: #fff;
      font-size: 30px;
      color: #fff;
      font-weight: 800;
    }
  }
}
.el-aside {
  background: -webkit-linear-gradient(rgb(38, 38, 177), rgb(134, 76, 76));
  .toggleMenu {
    width: 100%;
    height: 30px;
    line-height: 30px;
    text-align: center;
    color: #fff;
    background: rgb(25, 30, 177);
    letter-spacing: 2px;
    cursor: pointer;
    user-select: none;
  }
}
.el-main {
  background: #eaedf1;
}
.iconfont {
  margin-right: 5px;
}
</style>
